<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/headeradmin.php");
?>

<section>
<hgroup>
    <h1>Bewoner-detail bewerken</h1>
</hgroup>
<?php if(!isset($_POST['Submit'])) { 
    
    //Een 
    $query1 = "SELECT * FROM patientspec WHERE Id=".$_GET['id']."";
    $result1 = mysql_query($query1) or die (mysql_error());
    while ($row1 = mysql_fetch_array($result1))
    {
        $Id = $row1['Id'];
        $PatientKey = $row1['PatientKey'];
        $Comment = $row1['Comment'];
    }

    
    ?>
<form  name="buildingdetailform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <table>
        <tr>
            <td>Gegevens-veld</td>
            <td><input required placeholder="bv: breedte gebouw" name="detail" value="<?php echo $PatientKey; ?>" type="text"></td>
        </tr>
        <tr>
            <td>Commentaar</td>
            <td><input required placeholder="bv: breedte in meter" value="<?php echo $Comment; ?>" name="comment" type="text"></td>
        </tr>
 	<tr>
            <td><input type="submit" name="Submit" value="Aanpassen">
            <input type="hidden" name="detailid" value="<?php echo $Id ?>" />
            <input type="hidden" name="start" value="<?php if (isset($_GET['start'])){ echo $_GET['start']; } else { echo 0; }  ?>" /></td>
            <td><input type="button" value="Terug" onclick="javascript:history.back();" /></td>
        </tr>
</table>
</form>
    <?php 
} 
else {
    
    $error = array();
        
        if($_POST['detail'] == "") {
		$error[] = 'Het gegevens-veld is niet ingevuld.';
	}
        
        if($_POST['comment'] == "") {
		$error[] = 'Je moet extra commentaar toevoegen.';
	}
        
        $errors = sizeof($error); // aantal errors tellen
        if($errors != 0) { // Er is minstens 1 error
                echo "<span style=\"color:red\">Dit bewoner-detail kan niet worden aangepast omwille van de volgende reden(en):</span>";
                echo "<ul>";
                        for($i = 0; $i < $errors; $i++) {
                                echo "<span style=\"color:red\"><li>".$error[$i]."</li></span>";
                        }
                echo "</ul>";
        } 
        else 
        {
            $query = "UPDATE patientspec SET PatientKey='$_POST[detail]', Comment='$_POST[comment]' WHERE Id = '$_POST[detailid]'";
            mysql_query($query) or die ("FOUT2: " . mysql_error());
            echo "Het bewoner-detail is aangepast.<br>";
            echo "Klik <a href=\"patientdetailoverzicht.php?start=".$_POST['start']."\">hier</a> om terug te gaan naar het overzicht.";
        }   
}
?>
</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>